//
//  MineLoginViewController.m
//  陶瓷
//
//  Created by 郭天龙 on 15/6/14.
//  Copyright (c) 2015年 王戈. All rights reserved.
//

#import "MineLoginViewController.h"
#import "RegisterWithPhoneNoController.h"
#import "WPString.h"
#import "UserManager.h"
#import "ForgetPSWViewController.h"

@interface MineLoginViewController () <UITextFieldDelegate>

@property (nonatomic, weak) IBOutlet UITextField *userNameText;
@property (nonatomic, weak) IBOutlet UITextField *passWordText;
@property (nonatomic, weak) IBOutlet UIButton    *loginInButton;
@property (nonatomic, weak) IBOutlet UIButton    *registButton;
@property (nonatomic, weak) IBOutlet WPHotspotLabel    *findPassWordButton;
@property (nonatomic, weak) IBOutlet UIView *titlebg;

@property (nonatomic, strong) UIButton *cover;
@end

@implementation MineLoginViewController

-(IBAction)clickForgetPasswordButton:(id)sender
{
    ForgetPSWViewController *forgetVC = [[ForgetPSWViewController alloc] init];
    [self.navigationController pushViewController:forgetVC animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"登陆";
    self.loginInButton.layer.cornerRadius = 10;
//    self.loginInButton.layer.borderWidth = 1.5f;
//    self.loginInButton.layer.borderColor = [UIColor colorWithString:@"#666666"].CGColor;
    CGSize loginshadow = self.loginInButton.frame.size;
    loginshadow.height += 2;
    loginshadow.width += 2;
    self.loginInButton.layer.shadowOffset = loginshadow;
    self.loginInButton.layer.shadowColor = [UIColor whiteColor].CGColor;
    
//    _titlebg.layer.cornerRadius = 55;
//    _titlebg.layer.borderColor = [UIColor colorWithString:@"#666666"].CGColor;
//    _titlebg.layer.borderWidth = 2;
    
    
    _userNameText.delegate = self;
    _userNameText.keyboardType = UIKeyboardTypeNumberPad;
    _passWordText.delegate = self;
    _passWordText.secureTextEntry = YES;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationItem setTitle:@"登录"];
//    NSDictionary* style = @{@"body":[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0],
//                             @"Action":[WPAttributedStyleAction styledActionWithAction:^{
//                                 RegisterWithPhoneNoController *registerVC = [[RegisterWithPhoneNoController alloc] init];
//                                 [self.navigationController pushViewController:registerVC animated:YES];
//                             }],
//                             @"link":@[[UIColor blueColor],@{NSUnderlineStyleAttributeName : @(kCTUnderlineStyleSingle|kCTUnderlinePatternSolid)}]
//                             };
    [self.registButton addTarget:self action:@selector(registerVC:) forControlEvents:UIControlEventTouchUpInside];
//    self.registButton.attributedText = [@"<Action>手机快速注册</Action>" attributedStringWithStyleBook:style];
}

- (void)registerVC:(UIButton *)button
{
    RegisterWithPhoneNoController *registerVC = [[RegisterWithPhoneNoController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationItem setTitle:@""];
    [self.cover removeFromSuperview];
    [SVProgressHUD dismiss];
}

- (void)dealloc
{
    ;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)viewBtnClick:(UIButton *)sender {
    if ([self checkLogin]) {
        [self login];
    }
}

- (BOOL)checkLogin {
    if (!_userNameText.text.length) {
        [STAlertView st_promptAlertWithMessage:@"请输入用户名 !"];
        return NO;
    }
    if (!_passWordText.text.length) {
        [STAlertView st_promptAlertWithMessage:@"请输入密码 !"];
        return NO;
    }
    return YES;
}

- (void)login {
    
    [SVProgressHUD showWithStatus:@"正在登录.. 请稍候"];
    [self.view addSubview:self.cover];
    
    [[TCNetworkTool sharedNetTool] tokenWithEmail:_userNameText.text password:_passWordText.text andSuccessBlocks:^(NSDictionary *successMessage) {
        
        [SVProgressHUD dismiss];
        [SVProgressHUD showSuccessWithStatus:@"登录成功"];
        [self.cover removeFromSuperview];
        
        [[NSUserDefaults standardUserDefaults] setValue:@"YES" forKey:LOGIN_STATE];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    } andFailureBlocks:^(NSString *failureMessage) {
        [MBHUDHelper showWarningWithText:failureMessage];
        [SVProgressHUD dismiss];
        [self.cover removeFromSuperview];
    }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if ([alertView.title isEqualToString:@"注册成功 !"]) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (UIButton *)cover {
    if (_cover == nil) {
        _cover = [[UIButton alloc] initWithFrame:self.view.bounds];
        _cover.backgroundColor = [UIColor clearColor];
    }
    return _cover;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == _userNameText) {
        [_passWordText becomeFirstResponder];
    } else if (textField == _passWordText) {
        [self viewBtnClick:nil];
    }
    return  YES;
}
@end
